/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jsForms.Components.SourceEditor = {
	InitEditor:function()
	{
		var P = this.P;
		if (P.IsInitStarted == true)
		{
			return;
		}
		P.IsInitStarted = true;
		function initCycle(){
			var editor = P.Window.Editor;
			if (editor == undefined) {
				//jsForms.error('Editor not initialized');
				setTimeout(initCycle,100);
				return;
			}
			P.IsInit = true;
			
			switch (P.InitParam.syntax) {
				case 'html':
					editor.setLanguage(P.Window.htmlParser);
					break;
				case 'css':
					editor.setLanguage(P.Window.cssParser);
					break;
			}
			if (P.Editor.Text != undefined)
			{
				editor.setCode(P.Editor.Text);
				delete (P.Editor);
			}
			P.Editor = editor;
		}
		setTimeout(initCycle,100);
	},
	Init:function()
	{
		var frame = this.C.frame[0];//$('<iframe src="MDK-Editor/index.html"/>').appendTo(this)[0];
		var win=frame.contentWindow || frame.window;
		var doc=frame.contentDocument || frame.document;
		this.P.Window = win; 
		this.P.IsInit = false;
		this.P.Editor={
			setCode:function(str)
			{
				this.Text = str;
			},
			getCode:function(str)
			{
				return this.Text;
			}
		};
		if (this.P.InitParam == undefined)
		{
			this.P.InitParam={};
		}
	},
	Show:function()
	{
		return this;
	},
	Hide:function()
	{
		return this;
	},
	SetSource: function(newValue){
		if (this.P.IsInit == false) {
			this.InitEditor();
		}
		this.P.Editor.setCode(newValue);
		return this;
	},
	GetSource:function()
	{
		if (this.P.IsInit == false) {
			this.InitEditor();
		}
		return this.P.Editor.getCode();
	}
};

jsForms.CreateCompileInfoByObject(
{
	Name: 'SourceEditor',
	Properties: {
		InitParam: {
			Type: 'object',
			Access: 'direct'
		}
	},
	Events: {
	}
});